package com.colorful.insuranceproject.mine;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ListView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.adapter.DeliveryAdapter;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.DeliveryBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.MineRequest;
import com.lovely3x.common.adapter.ListAdapter;
import com.lovely3x.common.utils.Response;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/21.
 * <p>
 * 收获地址
 */
public class DeliveryActivity extends TitleActivity implements ListAdapter.ViewClickedListener {

    private static final int GET_DELIVERY_URL = 0X1;

    private static final int SET_DEFAULT_DELIVERY = 0X2;

    private static final int DELETE_DELIVERY = 0X3;

    private static final int REQUEST_CODE = 10000;

    private DeliveryAdapter deliveryAdapter;

    private MineRequest mineRequest;

    @Bind(R.id.lv_delivery)
    ListView lvDelivery;

    private int position;

    @Override
    protected int getContentView() {
        return R.layout.activity_delivery;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.delivery_address));
    }

    @Override
    protected void onViewInitialized() {
        mineRequest = new MineRequest(getHandler());
        deliveryAdapter = new DeliveryAdapter(null, mActivity);
        lvDelivery.setAdapter(deliveryAdapter);
        deliveryAdapter.setOnViewClickedListener(this);
        mineRequest.getDeliverUrl(GET_DELIVERY_URL);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        dismissProgressCircle();
        switch (msg.what) {
            case GET_DELIVERY_URL:
                if (response.isSuccessful) {
                    deliveryAdapter.setData((List<DeliveryBean>) response.obj);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;

            case SET_DEFAULT_DELIVERY:
                if (response.isSuccessful) {
                    for (DeliveryBean bean : deliveryAdapter.getDatas()) {
                        if (bean.getIsDefault().equals("1")) {
                            bean.setIsDefault("0");
                        }
                    }
                    for (int i = 0; i < deliveryAdapter.getDatas().size(); i++) {
                        if (i == position) {
                            deliveryAdapter.getDatas().get(i).setIsDefault("1");
                        }
                    }
                    deliveryAdapter.notifyDataSetChanged();
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;

            case DELETE_DELIVERY:
                if (response.isSuccessful) {
                    deliveryAdapter.getDatas().remove(position);
                    deliveryAdapter.notifyDataSetChanged();
                    showToast(getString(R.string.delete_delivery_success));
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @OnClick(R.id.tv_add_delivery)
    void OnClicked() {
        launchActivityForResult(AddDeliveryActivity.class, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            mineRequest.getDeliverUrl(GET_DELIVERY_URL);
        }
    }

    @Override
    public void onViewClicked(int positions, View view) {
        DeliveryBean bean = deliveryAdapter.getItem(positions);
        position = positions;
        switch (view.getId()) {
            case R.id.img_one://编辑
                launchActivityForResult(AddDeliveryActivity.class, REQUEST_CODE, AddDeliveryActivity.ADDRESS_DETAILS, bean);
                break;
            case R.id.img_two://删除
                final String id = bean.getAddressId();
                new AlertDialog.Builder(mActivity)
                        .setTitle(getString(R.string.hint))
                        .setMessage(getString(R.string.confrim_delete_delivery))
                        .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                showProgressCircle();
                                mineRequest.deleteDelivery(id, DELETE_DELIVERY);
                            }
                        }).setNegativeButton(getString(R.string.cancel), null).show();
                break;
            case R.id.tv_default://设置默认
                if (!bean.getIsDefault().equals("1")) {
                    showProgressCircle();
                    mineRequest.setDefaultDelivery(bean.getAddressId(), SET_DEFAULT_DELIVERY);
                }
                break;
        }
    }
}
